<?php

/*************************************************************************
 *
 *  System Name     ： UI TOOL
 *  Include Name  	： class.db.php
 *  Include IＤ		： db
 *
 *  Writer          ： Minz
 *  Create Date		： 2011/10/20
 *
 *************************************************************************/


class db{

//	var $mysql_host = "localhost";
//	var $mysql_user = "root";
//	var $mysql_pass = "apmsetup";
//	var $mysql_db 	= "test";

	var $mysql_host = "211.232.57.38";
	var $mysql_user = "root";
	var $mysql_pass = "saltlux";
	var $mysql_db 	= "test_db";
//	var $mysql_db 	= "test_db";

	var $connection = "";
	var $selectTotal = 0;

	function conn() {
		$this->connection = mysql_connect($this->mysql_host, $this->mysql_user, $this->mysql_pass)
		or die("connect error!!" . mysql_error());

		mysql_select_db($this->mysql_db,$this->connection);
		mysql_query("SET NAMES utf8");
	}

	function query($query) {

		global $debug_mode_value;

		$sql_query = $query;
		if(!empty($debug_mode_value)){
			echo("<br>$sql_query<br>");
		}
		$sql_result = mysql_query($query,$this->connection);
		if($sql_result) {
			return $sql_result;
		} else {
			$error = mysql_error($this->connection);
			if( $error ) {
				echo $error;
				$error_info = array();
				$error_info['no'] = mysql_errno($this->connection);
				$error_info['msg'] = $error;
				return $error_info;
			}
		}
	}
	function num($select) {
		$num =  @mysql_num_rows($select);
		return $num;
	}

	function fetch($select)
	{
		$sql_query = $this->query($select);
		$sql_fetch_array = mysql_fetch_array($sql_query);
		return $sql_fetch_array;
	}
	function assoc($select)
	{
		$sql_query = $this->query($select);
		$sql_fetch_array = mysql_fetch_assoc($sql_query);
		return $sql_fetch_array;
	}

	function fetch_array($select)
	{
		$sql_fetch_array = mysql_fetch_array($select);
		return $sql_fetch_array;
	}

	function fetch_assoc($select)
	{
		$sql_fetch_array = mysql_fetch_assoc($select);
		return $sql_fetch_array;
	}

	function in($string)
	{
		$string = mb_ereg_replace('"', '&quot;', $string);
		$string = mb_ereg_replace('\'', '&#039;', $string);
//		$string =strip_tags($string,"<img><font>");
		$con_string=iconv("shift-jis","utf-8",$string);

		return $con_string;
	}
	function in2($string)
	{
		$string = mb_ereg_replace('"', '&quot;', $string);
		$string = mb_ereg_replace('\'', '&#039;', $string);
//		$string =strip_tags($string,"<img><font>");
		$con_string=mb_convert_encoding($string, "utf-8", "auto");

		return $con_string;
	}

	function in_all($in){
		$key = array_keys($in);
		for($i=0; $i<count($key); $i++){
			$in[$key[$i]] = mb_ereg_replace('"', '&quot;', $in[$key[$i]]);
			$in[$key[$i]] = mb_ereg_replace('\'', '&#039;', $in[$key[$i]]);
//			$in[$key[$i]] =strip_tags($in[$key[$i]],"<img><font>");
//			$out[$key[$i]]= iconv("shift-jis","utf-8",$in[$key[$i]]);
		}
		return $in;
	}

	function in_all2($in){
		$key = array_keys($in);
		for($i=0; $i<count($key); $i++){
			$in[$key[$i]] = mb_ereg_replace('"', '&quot;', $in[$key[$i]]);
			$in[$key[$i]] = mb_ereg_replace('\'', '&#039;', $in[$key[$i]]);
//			$in[$key[$i]] =strip_tags($in[$key[$i]],"<img><font>");
			$out[$key[$i]]= mb_convert_encoding($in[$key[$i]],"utf-8","auto");
		}
		return $out;
	}

	function out($string,$flg=0)
	{
		if($flg==1){
			$string = mb_ereg_replace( '&quot;', '"',$string);
			$string = mb_ereg_replace( '&#039;','\'', $string);
		}
		$con_string=iconv("utf-8","shift-jis",$string);
		return $con_string;
	}

	function out2($string,$flg=0)
	{
		if($flg==1){
			$string = mb_ereg_replace( '&quot;', '"',$string);
			$string = mb_ereg_replace( '&#039;','\'', $string);
		}
		$con_string=mb_convert_encoding($string,"shift-jis","auto");
		return $con_string;
	}

	function out_all($in,$flg=0){
		$key = array_keys($in);
		for($i=0; $i<count($key); $i++){
			if($flg==1){
				$in[$key[$i]] = mb_ereg_replace('&quot;','"',  $in[$key[$i]]);
				$in[$key[$i]] = mb_ereg_replace('&#039;','\'',  $in[$key[$i]]);
			}
//			$out[$key[$i]]= iconv("utf-8","shift-jis",$in[$key[$i]]);
		}
		return $in;
	}

	function out_all2($in,$flg=0){
		$key = array_keys($in);
		for($i=0; $i<count($key); $i++){
			if($flg==1){
				$in[$key[$i]] = mb_ereg_replace('&quot;','"',  $in[$key[$i]]);
				$in[$key[$i]] = mb_ereg_replace('&#039;','\'',  $in[$key[$i]]);
			}
			$out[$key[$i]]= mb_convert_encoding($in[$key[$i]],"shift-jis","auto");
		}
		return $out;
	}
	function close()
	{
		mysql_close($this->connection);

	}

	function select( $select  )
	{
		$sqlData = array();

		$rs = $this->query( $select  );
		$num = $this->num($rs);
		$this->selectTotal = $num;
		if( $num > 0 )
		{
			for( $i = 0; $i < $num; $i++)
			{
				$sqlData[$i] = $this->fetch_assoc($rs);
			}
		}
		else
		{
			$sqlData = "";
		}
		return $sqlData;
	}


    function paging (array $arr)
    {
        //int total 	: total row
        //int page  	: now page
        //int row  		: row of one page
        //int scale 	: number of paging (block number) ex> << < 1 2 3 4 5 > >>  => $arr['scale']=5;
        //string link 	: "?page=xx" after link  ex> $arr['link']="&a=$a&b=$b";

    	$total = intval($arr['total']);
        $page = intval($arr['page']);
        $row = intval($arr['row']);
        $scale = intval($arr['scale']);
        $link = $arr['link'];
		$link2 = $arr['link2'];
		$admin = $arr['admin'];
		$start_low = $total-(($page-1)*$row);

        if($link) $amp = '';

        if($page < 1) $page = 1;
        $totalpage = ceil($total / $row);
        if($totalpage < $page) $page = $totalpage;
        $offset = ($page - 1) * $row;
        $limit = $row;
        if($total > 0) {
            $start = (intval($page / $scale) * $scale) + 1;
            if(!($page % $scale)) $start -= $scale;
            $end = $start + $scale - 1;
            if($end > $totalpage) $end = $totalpage;
            $prev = $start - $scale;
            $next = $start + $scale;
            $back = $page - 1;
            $after = $page + 1;
        }

        for($i = $start; $i <= $end; $i++)
            $paging_loop[] = array('page_num' => $i,
                                    'page_link' => "page=".$i.$amp.$link);

        $count = count($paging_loop);
        if($count > 0) $paging_loop[$count-1]['end'] = 1;

        $enable_prev = ($page > $scale) ? true : false;
        $enable_next = ($end < $totalpage) ? true : false;
		$enable_back = ($page > 1) ? true : false;
        $enable_after = ($page < $totalpage) ? true : false;


		################# PAGING HTML SETTING ##################
		$page_list="";
		if ($enable_back){
			if(!empty($admin)){
				$page_list.=" <a href='".$_SERVER["PHP_SELF"]."?page=1".$amp.$link."'>
						first
						</a>&nbsp;"; //<img src='../_image/btn_list_prev.gif' align='absmiddle' style='margin: 1px 0px 0px 0px;'/>
			}else{
			$page_list.=" <a href='".$_SERVER["PHP_SELF"]."?page=1".$amp.$link."'>
						first
						</a>&nbsp;"; //<img src='../_image/btn_list_prev.gif' align='absmiddle' style='margin: 1px 0px 0px 0px;'/>
			}
		}

		if ($enable_prev){
			if(!empty($admin)){
				$page_list.="<a href='".$_SERVER["PHP_SELF"]."?page=".$prev.$amp.$link."'>
						＜＜
						</a>&nbsp;"; //<img src='../_image/btn_list_pprev.gif'  align='absmiddle' style='margin: 1px 0px 0px 0px;'/>
			}else{
				$page_list.="<a href='".$_SERVER["PHP_SELF"]."?page=".$prev.$amp.$link."'>
						＜＜
						</a>&nbsp;"; //<img src='../_image/btn_list_pprev.gif'  align='absmiddle' style='margin: 1px 0px 0px 0px;'/>
			}
		}
		if ($enable_back){
			if(!empty($admin)){
				$page_list.=" <a href='".$_SERVER["PHP_SELF"]."?page=".$back.$amp.$link."'>
						◀
						</a>&nbsp;"; //<img src='../_image/btn_list_prev.gif' align='absmiddle' style='margin: 1px 0px 0px 0px;'/>
			}else{
			$page_list.=" <a href='".$_SERVER["PHP_SELF"]."?page=".$back.$amp.$link."'>
						◀
						</a>&nbsp;"; //<img src='../_image/btn_list_prev.gif' align='absmiddle' style='margin: 1px 0px 0px 0px;'/>
			}
		}
		for($i = $start; $i <= $end; $i++){
			if($i==$page){
				$page_list.=" <b> $i </b> ";
			}else{
				$page_list.="<a href='".$_SERVER["PHP_SELF"]."?page=".$i.$amp.$link."'> $i </a>";
			}
			if($i != $end){ $page_list .= "<font style='font-size:10px;color:#666666'>&nbsp;|&nbsp;&nbsp;</font>";}
		}
		if ($enable_after){
			if(!empty($admin)){
				$page_list.="&nbsp;<a href='".$_SERVER["PHP_SELF"]."?page=".$after.$amp.$link."'>
						▶
						</a>&nbsp;"; //<img src='../_image/btn_list_next.gif' align='absmiddle' style='margin: 3px 0px 0px 0px;'/>
			}else{
			$page_list.="&nbsp;<a href='".$_SERVER["PHP_SELF"]."?page=".$after.$amp.$link."'>
						▶
						</a>&nbsp;"; //<img src='../_image/btn_list_next.gif' align='absmiddle' style='margin: 3px 0px 0px 0px;'/>
			}
		}
		if ($enable_next){
			if(!empty($admin)){
				$page_list.="<a href='".$_SERVER["PHP_SELF"]."?page=".$next.$amp.$link."'>
						＞＞
						</a>&nbsp;"; //<img src='../_image/btn_list_nnext.gif' align='absmiddle' style='margin: 3px 0px 0px 0px;'/>
			}else{
			$page_list.="<a href='".$_SERVER["PHP_SELF"]."?page=".$next.$amp.$link."'>
						＞＞
						</a>&nbsp;"; //<img src='../_image/btn_list_nnext.gif' align='absmiddle' style='margin: 3px 0px 0px 0px;'/>
			}
		}
		if ($enable_after){
			if(!empty($admin)){
				$page_list.="&nbsp;<a href='".$_SERVER["PHP_SELF"]."?page=".$totalpage.$amp.$link."'>
						last
						</a>&nbsp;"; //<img src='../_image/btn_list_next.gif' align='absmiddle' style='margin: 3px 0px 0px 0px;'/>
			}else{
			$page_list.="&nbsp;<a href='".$_SERVER["PHP_SELF"]."?page=".$totalpage.$amp.$link."'>
						last
						</a>&nbsp;"; //<img src='../_image/btn_list_next.gif' align='absmiddle' style='margin: 3px 0px 0px 0px;'/>
			}
		}

        $ret = array('total' => $total,
        			'start_row' => $start_low,
                    'page' => $page,
					'self_link' => "page=$page".$amp.$link,
					'self_link2' => "page=$page".$amp.$link2,
                    'totalpage' => $totalpage,
                    'prev_link' => "page=$prev".$amp.$link,
                    'paging' => $paging_loop,
                    'next_link' => "page=$next".$amp.$link,
                    'start_link' => "page=1".$amp.$link,
                    'end_link' => "page=$totalpage".$amp.$link,
                    'enable_prev' => $enable_prev,
                    'enable_next' => $enable_next,
                    'enable_back' => $enable_back,
                    'enable_after' => $enable_after,
        			'back_link' => "page=$back".$amp.$link,
                    'after_link' => "page=$after".$amp.$link,
                    'limit' => $limit,
                    'offset' => $offset,
                    'page_list' => $page_list
            );
        return $ret;
    }

	/*
	 * 답변형 게시판 답변 번호(ORDER_NO) 가져오기
	 *   - 최신 답변이 밑으로 달리는 형식
	 *
	 * 게시판 테이블  형식
	 * seq			- 고유번호
	 * GROUP_NO		- 그룹번호  메인글의 seq넘버
	 * ORDER_NO		- 그룹 안에서의 소트 넘버
	 * DEPTH_NO		- 답변 깊이
	 *
	 * 파라미터
	 * $table,$group_no,$order_no,$depth_no 게시판 테이블 컬럼과 동일
	 *
	 * writer 	gm-kim
	 *
	*/
    function reply_order_no($table,$group_no,$order_no,$depth_no){
    	$query="SELECT IFNULL(MIN(ORDER_NO),( SELECT MAX(ORDER_NO)+1 FROM $table WHERE GROUP_NO = $group_no ))AS NO " .
    			" FROM $table " .
    			" WHERE GROUP_NO = $group_no " .
    			" AND ORDER_NO > $order_no " .
    			" AND DEPTH_NO <= $depth_no";
 		$sql_query = mysql_query($query,$this->connection);
		$sql_fetch_array = mysql_fetch_array($sql_query);
		return $sql_fetch_array['NO'];
    }

	function get_category($depth, $cat1="" ,$sel="", $add_query=""){
		$sqlData = array();

		$add="";
		if ($depth==2 and !empty($cat1)){
			$add=" and PARENT_NO = $cat1 " ;
		}else if($depth!=1){
			return null;
		}

		$query = " SELECT CATEGORY_NO, CATEGORY_NM
				   FROM S4U_CATEGORY_TB
				   WHERE DEPTH = $depth $add $add_query
				   ORDER BY VIEW_ORDER ASC ";
		$rs = $this->query($query);
		$num = $this->num($rs);

		if( $num > 0 ){
			for( $i=0; $i<$num; $i++ ){
				$sqlData[$i]=$this->fetch_assoc($rs);
				if( strlen($sel)>0 && $sqlData[$i]['CATEGORY_NO']==$sel ){
					$sqlData[$i]['chk']=' selected="selected" ';
				}else{
					$sqlData[$i]['chk']='';
				}
			}
		}else{
			$sqlData = null;
		}
		return $sqlData;
	}

	function get_subject( $cat ,$sel="", $add_query=""){
		$sqlData = array();

		$query = " SELECT SUBJECT_NO, SUBJECT_NM
				   FROM s4u_subject_tb
				   WHERE SUBJECT_GROUP_NO = (SELECT SUBJECT_GROUP_NO FROM s4u_category_tb WHERE CATEGORY_NO=$cat ) $add_query
				   ORDER BY VIEW_ORDER ASC ";
		$rs = $this->query($query);
		$num = $this->num($rs);

		if( $num > 0 ){
			for( $i=0; $i<$num; $i++ ){
				$sqlData[$i]=$this->fetch_assoc($rs);
				if( strlen($sel)>0 && $sqlData[$i]['SUBJECT_NO']==$sel ){
					$sqlData[$i]['chk']=' selected="selected" ';
				}else{
					$sqlData[$i]['chk']='';
				}
			}
		}else{
			$sqlData = null;
		}
		return $sqlData;
	}



	function get_selectbox($sel=""){
		$sqlData = array();

		$query = " SELECT NER_CD, NER_NM
				   FROM ner_tb
				   ORDER BY NER_CD ASC ";
		$rs = $this->query($query);
		$num = $this->num($rs);
		if( $num > 0 ){
			for( $i=0; $i<$num; $i++ ){
				$sqlData[$i]=$this->fetch_assoc($rs);
				if( strlen($sel)>0 && $sqlData[$i]['NER_CD']==$sel ){
					$sqlData[$i]['chk']=' selected="selected" ';
				}else{
					$sqlData[$i]['chk']='';
				}
			}
		}else{
			$sqlData = "";
		}
		return $sqlData;
	}

	function get_selectCode($group_cd,$sel=""){
		$sqlData = array();

		$query = " SELECT CODE_KEY, CODE_VALUE
				   FROM code_tb WHERE GROUP_CD = $group_cd
				   ORDER BY CODE_KEY ASC ";
		$rs = $this->query($query);
		$num = $this->num($rs);
		if( $num > 0 ){
			for( $i=0; $i<$num; $i++ ){
				$sqlData[$i]=$this->fetch_assoc($rs);
				if( strlen($sel)>0 && $sqlData[$i]['CODE_KEY']==$sel ){
					$sqlData[$i]['chk']=' selected="selected" ';
				}else{
					$sqlData[$i]['chk']='';
				}
			}
		}else{
			$sqlData = "";
		}
		return $sqlData;
	}

	# for category list - by jmk ##
	function get_selectbox3( $no, $depth_no, $sel="" ){
		$sqlData = array();
		($no=='NULL')? $parent_query = "AND ParentNo is NULL " : $parent_query = "AND ParentNo=$no ";;
		$query = " SELECT CategoryNo as No,
					      ParentNo,
					      Depth,
					      CategoryMei as Name
				   FROM syouhincategory
				   WHERE Depth=$depth_no AND DeleteFlg=0 $parent_query
				   ORDER BY SeiretuNo, CategoryNo ASC ";
		$rs = $this->query($query);
		$num = $this->num($rs);
		if( $num > 0 ) {
			for( $i = 0; $i < $num; $i++) {
				$sqlData[$i] = $this->fetch_assoc($rs);
				if (!empty($sel) && $sqlData[$i]["No"]==$sel){
					$sqlData[$i]['chk']=' selected="selected"';
				}else{
					$sqlData[$i]['chk']='';
				}
			}
		} else {
			$sqlData = '';
		}
		return $sqlData;
	}

	function get_checkbox($no,$sel=""){
		$query = " SELECT CodeKey, CodeValue, Etc
				   FROM code
				   WHERE CodeGroup=$no
				   ORDER BY CodeKey ASC ";
		$list = $this->select($query);
		for( $i=0; $i<count($list); $i++ ){
			if( strlen($sel)>0 && $list[$i]['CodeKey']==$sel ){
				$list[$i]['chk']=' checked="checked" ';
			}else{
				$list[$i]['chk']='';
			}
		}
		return $list;
	}
	function makeNavi($in){
		$query ="SELECT shc3.CategoryNo AS CategoryNo3,
			       shc3.CategoryMei AS CategoryMei3,
			       shc3.Depth AS Depth,
			       shc2.CategoryNo AS CategoryNo2,
			       shc2.CategoryMei AS CategoryMei2,
			       shc1.CategoryNo AS CategoryNo1,
			       shc1.CategoryMei AS CategoryMei1
			  FROM    (   syouhincategory shc3
			           LEFT OUTER JOIN
			              syouhincategory shc2
			           ON shc3.ParentNo = shc2.CategoryNo)
			       LEFT OUTER JOIN
			          syouhincategory shc1
			       ON shc3.GroupNo = shc1.CategoryNo
			 WHERE shc3.CategoryNo = $in";
		$this->conn();
		$now_cat=$this->assoc($query);
		echoo($query);
		$this->close();
		$depth=$now_cat["Depth"];
		for($i=(3-$depth); $i<=3; $i++){
			$col1="CategoryNo".$i;
			$col2="CategoryMei".$i;
			$tmp_data["CategoryNo"]=$now_cat["$col1"];
			$tmp_data["CategoryMei"]=$now_cat["$col2"];
			$out[]=$tmp_data;
		}
		return $out;
	}
}
if(!is_object( $db ))
{
	$db = new db;
}

?>